<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>原型对象</title>
  </head>
  <body>
    <script>
      //   function Person(name, age, gender) {
      //     this.name = name;
      //     this.age = age;
      //     this.gender = gender;
      //     this.sayHello = function () {
      //       console.log("~Hi");
      //     };
      //   }
      //   var p1 = new Person();
      //   var p2 = new Person();
      //   console.log(p1.sayHello == p2.sayHello); //false 执行两次，创建了两个对象

      //   Person.prototype = function sayHello() {
      //     console.log("~Hi");
      //   };
      //   function Person(name, age, gender) {
      //     this.name = name;
      //     this.age = age;
      //     this.gender = gender;
      //   }
      //   var p1 = new Person();
      //   var p2 = new Person();
      //   console.log(p1.sayHello == p2.sayHello); //true 原型中去访问

      function Person() {}
      // 动态添加
      Person.prototype.sayHello = function () {
        console.log("aaaa");
      };
      // 创建实例p1
      var p1 = new Person();

      // 直接替换
      Person.prototype = {
        sayHello: function () {
          console.log("bbbb");
        },
      };
      // 创建实例p2
      var p2 = new Person();
      p1.sayHello();
      p2.sayHello();
    </script>
  </body>
</html>
